home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programmer's Power Pack / Delphi Volume 1.iso / s_to_z / tcontain / unit1.pas < prev   
Pascal/Delphi Source File  |  1996-09-15  |  4KB  |  155 lines

  1. unit Unit1;
  2.  
  3. (* Quick example of tObjectList's uses : Streaming & Iterators*)
  4.  
  5. interface
  6.  
  7. uses
  8.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  9.   Forms, Dialogs, StdCtrls;
  10.  
  11. type
  12.   TForm1 = class(TForm)
  13.     Label1: TLabel;
  14.     Label2: TLabel;
  15.     Label3: TLabel;
  16.     Button1: TButton;
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure Button1Click(Sender: TObject);
  19.     procedure FormDestroy(Sender: TObject);
  20.   private
  21.     { Private declarations }
  22.   public
  23.     { Public declarations }
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30. Uses tContain;
  31. {$R *.DFM}
  32.  
  33. type
  34.  tCl1 = Class(tObject)
  35.   Public
  36.    D:Integer;
  37.   Constructor Create;
  38.   Procedure   Store(S:tStream); virtual;
  39.   Procedure   Load(S:tStream); virtual;
  40.  end;
  41.  
  42.  tCl2 = Class(tCl1)
  43.   Constructor Create;
  44.  end;
  45.  
  46.  tCl3 = Class(tCl2)
  47.   B:Array[0..1024] of byte;
  48.   Constructor Create;
  49.   Procedure   Store(S:tStream); override;
  50.   Procedure   Load(S:tStream); override;
  51.  end;
  52.  
  53.  
  54. Constructor tCl1.Create;
  55. begin
  56.  D:=1;
  57. end;
  58.  
  59.  
  60. Procedure   tCl1.Store(S:tStream);
  61. begin
  62.  S.WriteBuffer(D,SizeOf(D));
  63. end;
  64.  
  65. Procedure   tCl1.Load(S:tStream);
  66. begin
  67.  S.ReadBuffer(D,SizeOf(D));
  68. end;
  69.  
  70.  
  71. Constructor tCl2.Create;
  72. begin
  73.  Inherited Create;
  74.  Inc(D);
  75. end;
  76.  
  77. Constructor tCl3.Create;
  78. begin
  79.  Inherited Create;
  80.  Inc(D);
  81.  FillChar(B,1024,#3);
  82. end;
  83.  
  84. Procedure   tCl3.Store(S:tStream);
  85. begin
  86.  Inherited Store(S);
  87.  S.WriteBuffer(B,SizeOf(B));
  88. end;
  89.  
  90. Procedure   tCl3.Load(S:tStream);
  91. begin
  92.  Inherited Load(S);
  93.  S.ReadBuffer(B,SizeOf(B));
  94. end;
  95.  
  96.  Var List:tObjectList;
  97.  
  98. procedure TForm1.FormCreate(Sender: TObject);
  99. Var P1,P2,P3:tCl1;
  100. begin
  101.  List:=tObjectList.Create;
  102.  
  103.  List.Insert(0,tCl1.Create); (* Put a tCl1 instance in position 0 *)
  104.  List.AddObject(tCl2.Create);(* Add a tCl2 instance *)
  105.  List.Insert(0,tCl3.Create); (* Put a tCl3 instance in position 0, pushing the
  106.                                 tCl1 & tcl2 instances to position 1 and 2  *)
  107.  
  108.  List.SaveToStream('\test.str'); (* Store all 3 objects by calling their Store methods *)
  109.  List.Free;                      (* Dispose of the 3 objects and frees itself *)
  110.  
  111.  List:=tObjectList.CreateFromStream('\test.str'); (* Re create from streamed file*)
  112.  
  113.  P1:=List.Items[0] as tCl1; (* Access each, after loading from stream *)
  114.  P2:=List.Items[1] as tCl1;
  115.  P3:=List.Items[2] as tCl1;
  116.  
  117.  (* Even if the P1,2 & 3 variables have been typecast to tCl1, they still
  118.     retain there original class, as shown by the call to
  119.     their ClassName method. (The ClassName method is inherited
  120.     from tObject) *)
  121.  Label1.Caption:=Format('%s : D=%d',[P1.ClassName,P1.D]);
  122.  Label2.Caption:=Format('%s : D=%d',[P2.ClassName,P2.D]);
  123.  Label3.Caption:=Format('%s : D=%d',[P3.ClassName,P3.D]);
  124. end;
  125.  
  126. procedure TForm1.Button1Click(Sender: TObject);
  127. (* This shows how to use the tObjectList iterators. Note that SeePx is
  128.    1- Embedded
  129.    2- Declared FAR
  130.   Note also the use of tCl1 which is the common ancestor *)
  131. Procedure SeePx(AP:tCl1); far;
  132.  begin
  133.   Case List.IndexOf(AP) of
  134.    0: Label1.Caption:=Format('%s instance size=%d',[Ap.ClassName,ap.InstanceSize]);
  135.    1: Label2.Caption:=Format('%s instance size=%d',[Ap.ClassName,Ap.InstanceSize]);
  136.    2: Label3.Caption:=Format('%s instance size=%d',[Ap.ClassName,Ap.InstanceSize]);
  137.   end;
  138.  
  139.  end;
  140. begin
  141.  List.ForEach(@SeePx);
  142. end;
  143.  
  144. procedure TForm1.FormDestroy(Sender: TObject);
  145. begin
  146.  If Assigned(List)
  147.   then List.Free;  (* This will Free all 3 objects before destroying list *) 
  148. end;
  149.  
  150. Initialization
  151.  RegisterClass(@tCl1.Load,@tCl1.Store,tCl1);
  152.  RegisterClass(@tCl2.Load,@tCl2.Store,tCl2);
  153.  RegisterClass(@tCl3.Load,@tCl3.Store,tCl3);
  154. end.
  155.